package util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbcp2.BasicDataSource;
/**
 * 与数据库相关操作的工具类
 * @author Administrator
 *
 */
public class DBConnection {
	private static String driver="com.mysql.jdbc.Driver";
	private static String url="jdbc:mysql://39.106.53.244:3306/supermarker?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false";
	private static String user="root";
	private static String password="Zyy19970222!@#";
	//连接池配置参数
	private static int initialSize=15;//初始连接数
	private static int maxTotal=40;//最大连接数
	private static int maxIdle=10;//最大闲置数
	private static int minIdle=5;//最小闲置数
	private static int maxWaitMillis=10000;//最大等待时间(ms)

	private static BasicDataSource bds;

	static{
		init();
	}
	//初始化
	private static void init(){
		try {
			bds = new BasicDataSource();
			//设置驱动连接参数
			bds.setDriverClassName(driver);
			bds.setUrl(url);
			bds.setUsername(user);
			bds.setPassword(password);
			//设置连接池参数
			bds.setInitialSize(initialSize);
			bds.setMaxTotal(maxTotal);
			bds.setMaxIdle(maxIdle);
			bds.setMinIdle(minIdle);
			bds.setMaxWaitMillis(maxWaitMillis);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	//获取连接
	public static Connection getConn() throws SQLException{
		if(bds == null || bds.isClosed()){
			init();
		}
		return bds.getConnection();
	}

	//关闭资源的方法
	public static void close(ResultSet rs,
							 PreparedStatement ps,Connection conn){
		try {
			if(rs != null)rs.close();
			if(ps != null)ps.close();
			if(conn != null)conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 封装的数据更新操作
	 * @param conn 连接对象
	 * @param sql  sql语句
	 * @param obj  参数
	 * @return
	 * @throws SQLException
	 */
	public static boolean executUpadte(Connection conn,String sql,Object...obj) throws SQLException{
		PreparedStatement ps = conn.prepareStatement(sql);
		for(int i=0;i<obj.length;i++){
			ps.setObject(i+1, obj[i]);
		}
		int i = ps.executeUpdate();
		return i>0?true:false;
	}

	//封装查询全部数据的方法
	public static <T> List<T> queryList(CallBack<T> call,String sql,Object...objs) throws SQLException{
		Connection conn = getConn();
		PreparedStatement ps = conn.prepareStatement(sql);
		for(int i=0;i<objs.length;i++){
			ps.setObject(i+1, objs[i]);
		}
		ResultSet rs = ps.executeQuery();
		List<T> list = call.getDatas(rs);
		close(rs,ps,conn);
		return list;
	}

	//封装根据条件查询单个数据的方法
	public static <T> T queryOne(CallBack<T> call,String sql,Object...objs) throws SQLException{
		Connection conn = getConn();
		PreparedStatement ps = conn.prepareStatement(sql);
		for(int i=0;i<objs.length;i++){
			ps.setObject(i+1, objs[i]);
		}
		ResultSet rs = ps.executeQuery();
		T t =  call.getData(rs);
		close(rs,ps,conn);
		return t;
	}

	//JDK1.8一下版本支持
	public static abstract class CallBack<T>{
		public List<T> getDatas(ResultSet rs){
			return null;
		}
		public T getData(ResultSet rs){
			return null;
		}
	}




}
